package com.wisemoresystem.seal.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ne.framework.data.page.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wisemoresystem.seal.base.service.BaseService;
import com.wisemoresystem.seal.model.t_user;
import com.wisemoresystem.seal.util.SpringTools;
import com.wisemoresystem.seal.util.md5.MD5;

@Service
public class UserService extends BaseService {

	// 所有员工
	public List<Map<String, Object>> selectUserLists(final Page page) {
		String sql = "select t.*,tb.* from t_user t "
				+ " left outer join t_bank tb on t.bank_id=tb._id "
				+ " where t.status=0 order by t.created desc";

		List<Map<String, Object>> list = jc.queryForList(sql, page);
		return list;
	}

	// 所有员工
	public List<Map<String, Object>> selectUserLists(String bank_id,
			final Page page) {
		String sql = "select t.*,tbj.name job from t_user t "
				+ " left outer join t_bank_job tbj on t.job_code=tbj.code "
				+ " where t.status=0 and bank_id=? order by t.created desc";

		List<Map<String, Object>> list = jc.queryForList(sql,
				new String[] { bank_id }, page);

		return list;
	}

	// 验证账号
	public List<Map<String, Object>> chkCode(String code) {
		String sql = "select t.* from t_user t where t.status=0 and code=? order by t.created desc";

		List<Map<String, Object>> list = jt.queryForList(sql,
				new String[] { code });
		return list;
	}

	// 验证Boeing账号
	public List<Map<String, Object>> chkboEing(String boeing) {
		String sql = "select t.* from t_user t where t.status=0 and boeing=? order by t.created desc";

		List<Map<String, Object>> list = jt.queryForList(sql,
				new String[] { boeing });
		return list;
	}

	// 员工所有信息
	public Map<String, Object> selectUserAllMap(String user_id) {
		String sql = "select t.*,tb.name bank_name,tb.code bank_code,tr.name role_name from t_user t "
				+ " left outer join t_bank tb on tb._id=t.bank_id and tb.status=0 "
				+ " left outer join t_role tr on tr._id=t.role_id and tr.status=0 "
				+ "where t.status=0 and t._id=? ";

		List<Map<String, Object>> list = jt.queryForList(sql,
				new String[] { user_id });
		Map<String, Object> map = new HashMap<String, Object>();
		map = list.get(0);
		return map;
	}

	// 所有员工
	public List<Map<String, Object>> selectUserVerifyList(final Page page) {
		String sql = "select t.* from t_user t where t.status=0 and user_flag not in('正常','停用') order by t.created desc";

		List<Map<String, Object>> list = jc.queryForList(sql, page);
		return list;
	}

	public t_user selectUserByUnameAndPwd(String code, String pwd) {

		String hql = "from t_user t where t.code=? and t.pwd=? and t.user_flag='正常' and t.status=0";
		t_user user = super.findObj(hql, code, MD5.md5(pwd));
		return user;
	}

	public t_user selectUserByBoing(String code) {

		String hql = "from t_user t where t.boeing=?  and t.user_flag='正常' and t.status=0";
		t_user user = super.findObj(hql, code);
		return user;
	}

	// 事务
	@Transactional
	public void testSave() {
		t_user u = new t_user();
		u.setUname("东子123");
		super.saveObj(u);
		System.err.println(u.get_id());

		// if (true) {
		// throw new RuntimeException("xxx");
		// }
	}

	public static void main(String[] args) {
		// UserService s = (UserService) SpringTools.getBean(UserService.class);
		//
		// s.testSave();
		//
		// t_user user = s.findObjById("4028817a4b250717014b25071bbc0000",
		// t_user.class);
		// System.err.println(user.getUname());
		// .getCreated());
		//
		// System.err.println(s.selectUserLists(new Page()));

	}

}
